package com.yinp.proapp.module.study.jiguangloginshare.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.constant.Constant;
import com.yinp.proapp.databinding.ActivityJiGuangLoginBinding;
import com.yinp.proapp.utils.GlideUtils;
import com.yinp.proapp.utils.SelectTakePhoto;
import com.yinp.proapp.utils.StatusBarUtil;

import java.util.HashMap;

import cn.jiguang.share.android.api.AuthListener;
import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatActionListener;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import cn.jiguang.share.android.model.BaseResponseInfo;
import cn.jiguang.share.android.model.UserInfo;
import cn.jiguang.share.qqmodel.QQ;
import cn.jiguang.share.qqmodel.QZone;
import cn.jiguang.share.wechat.Wechat;

/**
 * @Author: yinp
 * @Date: 2021/8/26
 * @Description:极光登录测试
 */
public class JiGuangLoginActivity extends AppBaseActivity<ActivityJiGuangLoginBinding> {
    String imageUrl;
    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            imageUrl = (String) msg.obj;
            GlideUtils.into(mContext, imageUrl, bd.ivHead);
        }
    };

    @Override
    protected void initViews() {
        bd.header.headerCenterTitle.setText("极光登录");
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        initClick(bd.header.headerBackImg, bd.tvLogin, bd.tvShare, bd.tvQZoneShare, bd.tvWeixinShare);

        boolean canQQ = JShareInterface.isClientValid(QQ.Name);
        boolean QQAuthorization = JShareInterface.isSupportAuthorize(QQ.Name);

    }

    SelectTakePhoto selectTakePhoto;

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == bd.header.headerBackImg) {
            finish();
        } else if (v == bd.tvLogin) {
            QQLogin();
        } else if (v == bd.tvShare) {
            selectTakePhoto = new SelectTakePhoto();
            selectTakePhoto.selectType(this, 1);
        } else if (v == bd.tvQZoneShare) {
            ShareParams shareParams = new ShareParams();
            shareParams.setShareType(Platform.SHARE_TEXT);
            shareParams.setText("qq控件分享");
            JShareInterface.share(QZone.Name, shareParams, null);
        } else if (v == bd.tvWeixinShare) {
            ShareParams shareParams = new ShareParams();
            shareParams.setShareType(Platform.SHARE_TEXT);
            shareParams.setText("Text");//必须
            JShareInterface.share(Wechat.Name, shareParams, null);
        }
    }

    private void QQLogin() {
        JShareInterface.authorize(QQ.Name, new AuthListener() {
            @Override
            public void onComplete(Platform platform, int i, BaseResponseInfo baseResponseInfo) {
                Log.d(TAG, "onComplete: ");
                switch (i) {
                    case Platform.ACTION_AUTHORIZING:
                        JShareInterface.getUserInfo(QQ.Name, new AuthListener() {
                            @Override
                            public void onComplete(Platform platform, int action, BaseResponseInfo data) {
                                switch (action) {
                                    case Platform.ACTION_USER_INFO:
                                        if (data instanceof UserInfo) {      //第三方个人信息
                                            String openid = ((UserInfo) data).getOpenid();  //openid
                                            String name = ((UserInfo) data).getName();  //昵称
                                            String imageUrl = ((UserInfo) data).getImageUrl();  //头像url
                                            int gender = ((UserInfo) data).getGender();//性别, 1表示男性；2表示女性
                                            //个人信息原始数据，开发者可自行处理
                                            String originData = data.getOriginData();
                                            Message msg = new Message();
                                            msg.obj = imageUrl;
                                            handler.sendMessage(msg);
                                            String toastMsg = "获取个人信息成功:" + data.toString();
                                            Log.d(TAG, "onComplete: " + toastMsg);
                                        }
                                        break;
                                }
                            }

                            @Override
                            public void onError(Platform platform, int i, int i1, Throwable throwable) {

                            }

                            @Override
                            public void onCancel(Platform platform, int i) {

                            }
                        });
                        break;
                }
            }

            @Override
            public void onError(Platform platform, int i, int i1, Throwable throwable) {
                Log.d(TAG, "onError: ");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Log.d(TAG, "onCancel: ");
            }
        });
    }

    private void QQShare(String path) {
        ShareParams shareParams = new ShareParams();
        shareParams.setImagePath(path);
        shareParams.setShareType(Platform.SHARE_IMAGE);
        JShareInterface.share(QQ.Name, shareParams, new PlatActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Log.d(TAG, "onComplete: ");
            }

            @Override
            public void onError(Platform platform, int i, int i1, Throwable throwable) {
                Log.d(TAG, "onError: ");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Log.d(TAG, "onCancel: ");
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (selectTakePhoto.type == Constant.TAKE_PHOTO) {
                GlideUtils.intoRadius(mContext, selectTakePhoto.imageUri, bd.ivHead, 100, true);
                QQShare(selectTakePhoto.imageUri.toString());
            } else if (selectTakePhoto.type == Constant.SELECT_PHOTO) {
                if (data != null) {
                    String url = selectTakePhoto.handleImageOnKitKat(mContext, data);
                    GlideUtils.intoRadius(mContext, url, bd.ivHead, 100, true);
                    QQShare(url);
                }
            }
        }
    }
}
